package com.ty.zls.widget;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ty.zls.R;

/**
 * @author Administrator
 * @createTime 2019/12/12 22:48
 */
public class MyConfirrmDialog extends DialogFragment {
    private  onClick mOnClick;
    private EditText mUsedWater;
    private EditText mWaterFee;

    public void setOnClick(MyConfirrmDialog.onClick onClick) {
        mOnClick = onClick;
    }

    public static MyConfirrmDialog getInstance(String usedWater, String waterFee) {
        MyConfirrmDialog myConfirrmDialog = new MyConfirrmDialog();
        Bundle bundle = new Bundle();
        bundle.putString("usedWater", usedWater);
        bundle.putString("waterFee", waterFee);
        myConfirrmDialog.setArguments(bundle);
        return myConfirrmDialog;
    }


    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null && getDialog().getWindow() != null) {
            Window window = getDialog().getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.CENTER;
            window.setBackgroundDrawableResource(R.drawable.shape_bg_confirm);
            window.setAttributes(lp);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_myconfirm, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        String usedWater = getArguments().getString("usedWater");
        String waterFee = getArguments().getString("waterFee");
        mUsedWater = (EditText) view.findViewById(R.id.et_used_water);
        mWaterFee = (EditText) view.findViewById(R.id.et_water_fee);
        TextView mBtnConfirm = (TextView) view.findViewById(R.id.btn_confirm);
        TextView mBtnCancel = (TextView) view.findViewById(R.id.btn_cancel);
        mUsedWater.setText(usedWater);
        mWaterFee.setText(waterFee);

        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClick != null) {
                    mOnClick.onClickConfirm(mUsedWater.getText().toString().trim(), mWaterFee.getText().toString().trim());
                    dismiss();
                }
            }
        });
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                onClick.onClickCancel();
                dismiss();
            }
        });
    }

    public interface onClick {
        void onClickConfirm(String usedWater, String waterFee);

//        void onClickCancel();
    }
}
